package com.hmall.trade.listener;


import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PayStatusListener {

    private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue", durable = "true"),
            exchange = @Exchange(name = "pay.direct"),
            key = "pay.success"
    ))
    //配置交换机、队列、交换机和队列的绑定关系、队列标签
    //队列名称 trade.pay.success.queue
    //交换机名称 pay.direct，默认direct类型
    //队列标签 pay.success

//    public void listenPaySuccess(Long orderId){
//        orderService.markOrderPaySuccess(orderId);
//    }

    public void listenPaySuccess(Long orderId){

        //1.查询订单
        Order order = orderService.getById(orderId);

        //2.判断订单
        if (order == null || order.getStatus() != 1){
            //不做处理
            return;
        }

        //3.标记订单状态为已支付
        orderService.markOrderPaySuccess(orderId);

    }


}
